package protectora.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Date;

import protectora.Perro;

public class AnimalesDAOImpl implements AnimalesDAO {

	public boolean insertarAnimal(String FechaEntrada, String Nombre, int Edad,
			boolean adoptado, boolean sociable, boolean enfermo,
			String idCasaDeAcogida, String idProtectora) {
		boolean resul = true;
		Connection conn = ConnectionManager.getInstance().checkOut();
		String casa = null;
		if(!idCasaDeAcogida.equals("")){
			casa = "'" + idCasaDeAcogida +"'";
		}
		
		PreparedStatement st = null;
		
		try {
			String sql = "INSERT INTO animales (FechaEntrada, Nombre, Edad, "
					+ "Adoptado, Sociable, Enfermo, IdCasaDeAcogida, "
					+ "IdProtectora)";
			sql += "Values('" + FechaEntrada + "' , '" + Nombre
					+ "' , '" + Edad + "'," + adoptado + "," + sociable + ","
					+ enfermo + "," + casa + ", '"
					+ idProtectora + "');";

			st = conn.prepareStatement(sql);
			resul = st.execute();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				st.close();
				conn.close();
				
			} catch (Exception e) {
				System.out.println("Error al cerrar conexion");
			}
		}
		return !resul;
	}

	public int getLastId() {
		Integer resul = null;
		Connection conn = ConnectionManager.getInstance().checkOut();
		
		ResultSet rs = null;
		PreparedStatement st = null;
		
		try {
			String sql = "SHOW TABLE STATUS WHERE Name = 'animales'";
			
			st = conn.prepareStatement(sql);
			rs=st.executeQuery();
			while(rs.next()){
				resul = rs.getInt("Auto_increment");
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				st.close();
				conn.close();
				
			} catch (Exception e) {
				System.out.println("Error al cerrar conexion");
			}
		}
		return resul;
	}

}
